package rtmp_key;

import java.util.Calendar;

public class Licence {

	public static byte[] bytesKeys= null;

	public static void setKey(String paramString) {
		setKey(a(paramString));
	}

	private static byte[] a(String paramString) {
		paramString = paramString.replaceAll("-", "");
		byte[] arrayOfByte = new byte[paramString.length()];
		int i = 0;
		int j = arrayOfByte.length;
		while (i < j) {
			try {
				arrayOfByte[i] = (byte) (Integer.parseInt(
						paramString.substring(i, i + 1), 16) & 0xF);
				System.out.print(" 0x" + Integer.toHexString(arrayOfByte[i]));
			} catch (Exception localException) {
			}
			i++;
		}
		System.out.println(" ");

		return arrayOfByte;
	}

	static void setKey(byte[] paramArrayOfByte) {
		if ((paramArrayOfByte == null) || (paramArrayOfByte.length != 25))
			return;
		bytesKeys = paramArrayOfByte;
	}


	private static void checkKeys(byte[] paramArrayOfByte)
			throws IllegalArgumentException {
		int m = 0;
		for (int n = 1; n < paramArrayOfByte.length - 1; n++)
			m += (paramArrayOfByte[n] & 0xFF);
		m &= 255;
		log("tatal num:" + Integer.toHexString(m));
		int n = paramArrayOfByte[1] & 0xF;
		if (((paramArrayOfByte[0] & 0xFF) != (byte) (m >> 0 & 0xF))
				|| ((paramArrayOfByte[(paramArrayOfByte.length - 1)] & 0xFF) != (byte) (m >> 4 & 0xF))
				|| (paramArrayOfByte[1]
						+ paramArrayOfByte[(paramArrayOfByte.length - 2)] != 15))
			a(16);
		boolean[] arrayOfBoolean = new boolean[21];
		byte[] arrayOfByte = new byte[8];
		int i1 = 1;
		int i2 = n;
		for (int i3 = 0; i3 < arrayOfByte.length; i3++) {
			while (arrayOfBoolean[(i2 % arrayOfBoolean.length)] != false)
				i2++;
			arrayOfBoolean[(i2 % arrayOfBoolean.length)] = true;
			arrayOfByte[i3] = paramArrayOfByte[(2 + i2 % arrayOfBoolean.length)];
			i1 += 2;
			i2 += i1;
		}
		for (int jj=0; jj <8; jj++)
			log(Integer.toHexString(arrayOfByte[jj]));
		if ((arrayOfByte[1] & 0xF) != 3)
			a(32);
		int i3 = (arrayOfByte[3] & 0xF) >= 8 ? 1 : 0;
		int i4 = (i3 != 0 ? arrayOfByte[3] - 8 : arrayOfByte[3]) & 0xF;
		if (i4 < 1)
			a(1);
		if (i3 != 0) {
			Calendar localCalendar = Calendar.getInstance();
			localCalendar.set(1, 2000 + (arrayOfByte[4] & 0xF));
			localCalendar.set(2, (arrayOfByte[5] & 0xF) - 1);
			localCalendar.set(5, ((arrayOfByte[6] & 0xF) << 4)
					+ (arrayOfByte[7] & 0xF));
			log(localCalendar.toString());
			if (System.currentTimeMillis() - localCalendar.getTimeInMillis() > 0L)
				a(18);
		}
	}

	private static void a(int paramInt) {
		switch (paramInt & 0xF) {
		case 0:
			throw new IllegalArgumentException("exception0");
		case 1:
			throw new IllegalArgumentException("exception2");
		case 2:
			throw new IllegalArgumentException("exception3");
		}
	}
	
	public static void log(String msg) {
		
		System.out.println(msg);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		setKey("7C00C-03A95-F1C05-023DA-75838");
		checkKeys(bytesKeys);
	}

}
